<script setup>
import {computed, ref} from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import store from "@/store";

const router = useRouter()
const form = ref({ username: '', password: '' })

const handleLogin = async () => {
  try {
    if (!form.value.username || !form.value.password) {
      return ElMessage.warning('请输入账号密码')
    }

    const { data } = await axios.post('http://localhost:8080/user/login', {
      no: form.value.username,
      password: form.value.password
    })

    if (data.code === 200) {
      localStorage.setItem('user', JSON.stringify(data.data.user))
      console.log('用户角色ID:', data.data.user.roleId); // 调试日志
      localStorage.setItem('menu', JSON.stringify(data.data.menu))
      console.log(data.data.menu)
      store.commit('setMenu', data.data.menu)
      await router.push('/home')
    } else {
      ElMessage.error(data.msg)
    }
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '登录失败')
  }
}
const handleRegister = () => {
  router.push('/register')
}
const getMenu = computed(() => store.state.menu)
</script>
<template>
  <div class="login-wrapper">
    <div class="login-card">
      <div class="login-header">
        <h2>欢迎登录</h2>
        <p>请使用您的账号登录系统</p>
      </div>

      <el-form :model="form" class="login-form">
        <el-form-item label="账号">
          <el-input
              v-model="form.username"
              placeholder="请输入您的账号"
              prefix-icon="el-icon-user"
          />
        </el-form-item>

        <el-form-item label="密码">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入您的密码"
              show-password
              prefix-icon="el-icon-lock"
              @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-btn" @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer">
        <el-link type="primary" @click="handleRegister">没有账号？立即注册</el-link>
        <el-link type="info" class="forgot-password" @click="handleForgotPassword">忘记密码？</el-link>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-wrapper {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
}

.login-card {
  width: 400px;
  padding: 30px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.login-header h2 {
  color: #333;
  font-size: 22px;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.login-form {
  margin-top: 20px;
}

.login-form .el-form-item {
  margin-bottom: 20px;
}

.login-form .el-input {
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 40px;
  background: linear-gradient(to right, #4facfe, #00f2fe);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s ease;
}

.login-btn:hover {
  background: linear-gradient(to right, #00c6fb, #005bea);
}

.footer {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  font-size: 13px;
}

.forgot-password {
  color: #888;
}

@media (max-width: 480px) {
  .login-card {
    width: 90%;
    padding: 20px;
  }
}
</style>
